package com.example.tests;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
//import java.util.Random;
import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class ContactModificationTests extends TestBase {
	
	@Test(dataProvider = "randomValidContactGenerator")
	public void modifySomeContact(ContactData contact) {
		// model
		SortedListOf<ContactData> contactListFromModel = app.getContactHelper().getContactsFromModel();
		
		// save old state from DB (cache)
		SortedListOf<ContactData> oldContactListFromDB = app.getContactHelper().getContactsFromCache();

		// get Contact Index to modify
		int index = getRandomIndex(contactListFromModel.size());
	    
	    // perform actions
		ContactFromFormData contactToModify = app.getContactHelper().modifyContact(contact, index);
		
		// save new state
	    SortedListOf<ContactData> newContactListFromUI = app.getContactHelper().getContacts();
	    SortedListOf<ContactData> newContactListFromDB = app.getContactHelper().getContactsFromCache();
	    
	    // compare states
	    assertThat(contactToModify, equalTo(oldContactListFromDB.get(index).toContactFromFormData()));
	    assertThat(newContactListFromUI, equalTo(newContactListFromDB));
	    assertThat(newContactListFromUI, equalTo(contactListFromModel));
	    assertThat(newContactListFromDB, equalTo(oldContactListFromDB.without(index).withAdded(contact)));
	}
}
